package com.lottery.im.module.service.ui;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.msg_bulletin.Bulletins;
import com.lottery.im.api.result.entity.msg_bulletin.Messages;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.BulletinAdapter;
import com.lottery.im.module.service.adapter.MessageAdapter;
import com.lottery.im.module.service.presenter.MessageAndBulletinPresenter;
import com.lottery.im.module.service.view.MessageAndBullentinView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;

public class MessageAndBulletinFragment extends BaseSwipeFragment<MessageAndBulletinPresenter>
    implements MessageAndBullentinView, OnRefreshListener, OnLoadMoreListener {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.tabLayout) CommonTabLayout tabLayout;
  @BindView(R.id.list_message) RecyclerView listMessage;
  @BindView(R.id.list_bulletin) RecyclerView listBulletin;
  @BindView(R.id.srf) SmartRefreshLayout srf;

  private String[] mTabNames = { "消息", "公告" };
  private BulletinAdapter mBulletinAdapter;
  private MessageAdapter mMessageAdapter;

  private int startRowMessage=0;//始查询消息条数
  private int startRowBulletins=0;
  private int pageSize=10;//查询条数

  @Override protected int getLayoutResId() {
    return R.layout.fragment_message_bulletin;
  }

  @Override protected void initPresenter() {
    presenter = new MessageAndBulletinPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    tvTitle.setText("消息公告");
    initTabs();

    initAdapter();

    presenter.getBulltins(startRowBulletins,pageSize,null);
    presenter.getMessages(startRowMessage,pageSize,null);

    srf.setOnRefreshListener(this);
    srf.setOnLoadMoreListener(this);
  }

  private void initAdapter() {
    listMessage.setLayoutManager(new LinearLayoutManager(mActivity));
    listBulletin.setLayoutManager(new LinearLayoutManager(mActivity));

    RecyclerViewDivider.with(mActivity)
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider().build().addTo(listMessage);
    RecyclerViewDivider.with(mActivity)
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider().build().addTo(listBulletin);

    mBulletinAdapter = new BulletinAdapter();
    mMessageAdapter = new MessageAdapter();

    listBulletin.setAdapter(mBulletinAdapter);
    listMessage.setAdapter(mMessageAdapter);

    //左侧消息列表点击
    mMessageAdapter.setOnItemClickListener(
        (position, object) ->
        {
          startFragment(new MessageAndBulletinDetailFragment(getString(R.string.message_detail),
              object.getId()));
          if(1!=object.getState()){
            object.setState(1);
            mMessageAdapter.notifyDataSetChanged();
          }
        });
    //右侧消息列表点击
    mBulletinAdapter.setOnItemClickListener(
        (position, object) ->
            startFragment(new MessageAndBulletinDetailFragment(getString(R.string.bulletin_detail), object.getBulletinId())));
  }

  private void initTabs() {
    ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
    for (int i = 0; i < mTabNames.length; i++) {
      final int position = i;
      tabEntities.add(new CustomTabEntity() {
        @Override public String getTabTitle() {
          return mTabNames[position];
        }

        @Override public int getTabSelectedIcon() {
          return 0;
        }

        @Override public int getTabUnselectedIcon() {
          return 0;
        }
      });
    }
    tabLayout.setTabData(tabEntities);
    tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
      @Override public void onTabSelect(int position) {
        if (position == 0) {
          listBulletin.setVisibility(View.GONE);
          listMessage.setVisibility(View.VISIBLE);
        } else {
          listMessage.setVisibility(View.GONE);
          listBulletin.setVisibility(View.VISIBLE);
        }
      }

      @Override public void onTabReselect(int position) {
      }
    });
  }

  @OnClick(R.id.iv_back) public void onViewClicked() {
    popBackStack();
  }

  //刷新左侧适配器
  @Override public void loadMessageSuccess(List<Messages> list) {
    mMessageAdapter.notifyList(list);
  }

  //刷新右侧适配器
  @Override public void loadBulletinSuccess(List<Bulletins> list) {
    mBulletinAdapter.notifyList(list);
  }

  //下拉刷新
  @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) {
    switch (tabLayout.getCurrentTab()){
      case 0://消息
        startRowMessage=0;
        presenter.getMessages(startRowMessage,pageSize,(list, isSuccess) -> {
          mMessageAdapter.notifyList(list);
          refreshLayout.finishRefresh(200,isSuccess);
        });
        break;
      case 1://公告
        startRowBulletins=0;
        presenter.getBulltins(startRowBulletins,pageSize,(list, isSuccess) -> {
          mBulletinAdapter.notifyList(list);
          refreshLayout.finishRefresh(200,isSuccess);
        });
        break;
    }
  }
  //上拉加载更多
  @Override public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
    switch (tabLayout.getCurrentTab()){
      case 0://消息
        startRowMessage=startRowMessage + pageSize;
        presenter.getMessages(startRowMessage, pageSize,
            (list, isSuccess) -> {
              mMessageAdapter.addList(list);
              refreshLayout.finishLoadMore(isSuccess);
              if(!isSuccess){
                startRowMessage=startRowMessage - pageSize;
              }
            });
        break;
      case 1://公告
        startRowBulletins=startRowBulletins + pageSize;
        presenter.getBulltins(startRowBulletins, pageSize,
            (list, isSuccess) -> {
              mBulletinAdapter.addList(list);
              refreshLayout.finishLoadMore(isSuccess);
              if(!isSuccess){
                startRowBulletins=startRowBulletins - pageSize;
              }
            });
        break;
    }
  }
}
